<style lang="less" scoped>
.conent{
  padding: 92px 0 10px;
  .row{
    display: flex;
    align-items: center;
    justify-content: space-between;
    height: 97px;
    font-size: 26px;
    color: #999;
    border-bottom: 1px solid #333E58;
    padding: 0 35px;
    input{
      background: rgba(0, 0, 0, 0);
      outline: none;
    }
  }
  .actionBtn{
    width: 607px;
    height: 82px;
    background: #FFA22A;
    line-height: 82px;
    text-align: center;
    margin: 73px auto 10px;
    font-size: 30px;
    color: #000
  }
}
</style>

<template>
<div>
  <top-bar/>
  <div class="conent">
    <div class="row">
      <input type="text" placeholder="请输入昵称" v-model="formData.nick">
    </div>
    <div class="actionBtn" @click="reset">提交</div>
  </div>
</div>
</template>

<script>
import { useStore } from "vuex"
import { defineComponent, provide, reactive } from "@vue/runtime-core"
import { updateUser } from '../../api/home'
import { Toast } from "vant"
import { useRouter } from "vue-router"
export default defineComponent({
  setup() {
    const store = useStore()
    provide('title', '修改昵称')
    const formData = reactive({
      nick: store.state.userMsg.nickName
    })
    const router = useRouter()


    const reset = async () => {
      if (!formData.nick) {
        Toast.fail('请输入昵称')
        return false
      }
      const loading = Toast.loading('设置中')
      const res = await updateUser({
        nickName: formData.nick
      })
      loading.clear()
      if (res.code === 200) {
        Toast.success('保存成功')
        store.commit('initUsermsg', res.data)
        try {
          router.go(-1)
        } catch {
          router.push({
            path: '/'
          })
        }
      } else {
        Toast.fail(res.msg)
      }
    }


    return {
      reset,
      formData,
    }
  }
})
</script>